import { lazyReportBatch } from '../export';

/**
 * 监听FCP
 * 首次绘制出非空白内容的时间点，即内容必须是 “有意义的”，例如文本、图片、非白色的元素等
 */
const observeFCP = () => {
  const entryHandler = (list) => {
    for (const entry of list.getEntries()) {
      if (entry.name === 'first-contentful-paint') {
        observer.disconnect();
        const json = entry.toJSON();
        console.log(json);
        const reportData = {
          ...json,
          type: 'performance',
          subType: entry.name,
          pageUrl: window.location.href,
          title: '首次内容绘制指标',
        };
        // 发送数据
        lazyReportBatch(reportData);
      }
    }
  };

  const observer = new PerformanceObserver(entryHandler);
  observer.observe({ type: 'paint', buffered: true });
};

export default observeFCP;
